package com.renchi.menu.Collection.service;

import com.renchi.menu.Collection.dao.CollectionDbDao;
import com.renchi.menu.Collection.entity.CollectionDbEntity;
import com.renchi.menu.Collection.vo.CollectionDbVo;
import com.renchi.menu.common.page.FastPage;
import com.renchi.menu.common.page.PageInfo;
import com.renchi.menu.common.result.Result;
import com.renchi.menu.common.result.ResultModel;
import com.renchi.menu.common.result.ResultTools;
import com.renchi.menu.common.util.BeanConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
@Slf4j
public class CollectionDbService {
    @Autowired
    private CollectionDbDao collectionDbDao;

    public Result addCollection(CollectionDbEntity collectionDbEntity){
        try {
            Date nowTime = new Date();
            java.sql.Date time = new java.sql.Date(nowTime.getTime());
            collectionDbEntity.setCreate_time(time);
            collectionDbEntity.setUpdate_time(time);
            collectionDbDao.save(collectionDbEntity);
            return ResultTools.result(0,"",null);
        }catch (Exception e){
            log.error("收藏菜谱失败");
            System.out.println(e.getMessage());
            return ResultTools.result(404,e.getMessage(),null);
        }
    }

    public ResultModel<PageInfo<CollectionDbVo>> findCollection(CollectionDbVo cVo){
        List<CollectionDbEntity> collectionDbEntities= collectionDbDao.findAllById(cVo.getUser_id());
        List<CollectionDbVo> collectionDbVos= BeanConvertUtils.convertVoCollection(collectionDbEntities,CollectionDbVo.class);

        //生成分页对象
        PageInfo<CollectionDbVo> pageInfo = FastPage.getListVoPageInfo(collectionDbVos,cVo.getPageSize(),cVo.getPage());
        return ResultModel.of(pageInfo);
    }

    public Result deldeCollection(String menu_url,String user_id){
        try {
            collectionDbDao.deleteCollectionDbEntities(menu_url, user_id);
            return ResultTools.result(0,"",null);
        }catch (Exception e){
            log.error("删除菜谱失败");
            System.out.println(e.getMessage());
            return ResultTools.result(404,e.getMessage(),null);
        }
    }
}
